/// @Classname : HcDialogButtonPanel
/// @Author :
/// @Time : 2024-03-18 13:45
/// @Description : //dialog或Sheet的按钮组

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/theme/hc_button_theme.dart';
import 'package:hc_ui/utils/hc_color_util.dart';
import 'package:hc_ui/widget/button/hc_base_button.dart';
import 'package:hc_ui/widget/divider/hc_divider.dart';
import 'package:hc_ui/widget/grid/hc_grid.dart';

enum HcButtonType {
  elevated,
  outlined,
  text,
}

///按钮的配置类
class HcButtonPanelConfig {
  //状态是否为禁用
  bool disabled;

  //按钮的名称
  String label;

  HcButtonPanelConfig({
    this.disabled = false,
    required this.label,
  });
}

/// Dialog或Sheet弹出组件的按钮面板
/// 横向最多展示两枚按钮超过两枚按钮纵向展示
/// 最多有一枚主按钮和多枚次级按钮
class HcDialogButtonPanel extends StatelessWidget {
  //排列方向
  final Axis decoration;

  //主按钮文字
  final String mainButtonText;

  //主按钮回调
  final VoidCallback mainButtonOnTap;

  //主按钮是否禁用
  final bool mainButtonDisabled;

  //主按钮的样式
  final HcButtonType mainButtonType;

  //次级按钮的样式
  final HcButtonType secondaryButtonType;

  //次级按钮列表
  final List<HcButtonPanelConfig>? secondaryButtonList;

  //次级按钮回调
  final void Function(int)? secondaryButtonOnTap;

  //按钮的间隔
  final double buttonSpacing;

  //按钮的圆角
  final double? radius;

  final EdgeInsets padding;

  final bool buttonGroupReverse;
  final Color? borderColor;
  final double? borderWidth;

  const HcDialogButtonPanel(
      {super.key,
      this.decoration = Axis.horizontal,
      this.secondaryButtonList,
      required this.mainButtonText,
      required this.mainButtonOnTap,
      this.secondaryButtonOnTap,
      required this.mainButtonDisabled,
      required this.buttonSpacing,
      this.radius,
      this.padding = const EdgeInsets.all(24),
      this.mainButtonType = HcButtonType.elevated,
      this.secondaryButtonType = HcButtonType.outlined,
      this.buttonGroupReverse = false,
      this.borderColor,
      this.borderWidth});

  @override
  Widget build(BuildContext context) {
    HcButtonTheme buttonTheme = HcCustomerConfig().allTheme.buttonTheme;

    //最终的排列方向 超过两个按钮纵向排列 如果次级按钮为文字纵向展示
    Axis finalDecoration =
        secondaryButtonList != null && secondaryButtonList!.length > 1 ||
                mainButtonType != HcButtonType.text &&
                    secondaryButtonType == HcButtonType.text
            ? Axis.vertical
            : decoration;
    //最终的按钮列表
    List<HcButtonPanelConfig> buttonList = [];

    if (secondaryButtonList != null) {
      buttonList.addAll(secondaryButtonList!.where((element) => true));
    }
    buttonList.insert(
        finalDecoration == Axis.vertical || buttonGroupReverse
            ? 0
            : buttonList.length,
        HcButtonPanelConfig(label: mainButtonText));

    ///圆角大小
    BorderRadius finalRadius = radius != null
        ? BorderRadius.circular(radius!)
        : buttonTheme.borderRadius;

    /// 是否是分隔符按钮
    bool isSplitBtn = mainButtonType == HcButtonType.text &&
        (buttonList.length == 1 || secondaryButtonType == HcButtonType.text);

    return Container(
      decoration: BoxDecoration(
          border: isSplitBtn
              ? Border(
                  top: BorderSide(
                      color: borderColor ?? buttonTheme.commonTheme.borderColor,
                      width: borderWidth ?? buttonTheme.commonTheme.lineBase))
              : null),
      padding: isSplitBtn ? EdgeInsets.zero : padding,
      child: HcGrid(
        crossAxisSpacing: isSplitBtn ? 0.0 : buttonSpacing,
        mainAxisSpacing: isSplitBtn ? 0.0 : buttonSpacing,
        dividerColor: isSplitBtn
            ? borderColor ?? buttonTheme.commonTheme.borderColor
            : Colors.transparent,
        dividerThickness:
            isSplitBtn ? borderWidth ?? buttonTheme.commonTheme.lineBase : 0.0,
        dividerStyle: HcDividerStyle.solid,
        itemBuilder: (BuildContext context, int index) {
          ///是不是主按钮
          bool isMainBtn = index ==
              (finalDecoration == Axis.horizontal && !buttonGroupReverse
                  ? buttonList.length - 1
                  : 0);
          return SizedBox(
            width: double.infinity,
            child: HcBaseButton(
              elevation: 0,
              themeData: _buildButtonTheme(
                  index: index,
                  isMainBtn: isMainBtn,
                  finalRadius: finalRadius,
                  finalDecoration: finalDecoration,
                  buttonTheme: buttonTheme,
                  buttonList: buttonList,
                  isSplitBtn: isSplitBtn),
              onPressed: () {
                if (isMainBtn) {
                  mainButtonOnTap.call();
                } else {
                  if (secondaryButtonOnTap != null) {
                    secondaryButtonOnTap!.call(index +
                        (finalDecoration == Axis.horizontal &&
                                !buttonGroupReverse
                            ? 1
                            : 0));
                  }
                }
              },
              text: buttonList[index].label,
              disabled: buttonList[index].disabled,
            ),
          );
        },
        crossAxisCount:
            finalDecoration == Axis.vertical ? 1 : buttonList.length,
        itemCount: buttonList.length,
      ),
    );
  }

  HcButtonTheme _buildButtonTheme(
      {required HcButtonTheme buttonTheme,
      required bool isSplitBtn,
      required bool isMainBtn,
      required int index,
      required BorderRadius finalRadius,
      required Axis finalDecoration,
      required List<HcButtonPanelConfig> buttonList}) {
    /// 最后的样式
    HcButtonTheme result;

    ///按钮的类型
    HcButtonType type = isMainBtn ? mainButtonType : secondaryButtonType;

    switch (type) {
      case HcButtonType.elevated:
        Color surfaceColor =
            HcColorUtil.needLightColor(buttonTheme.commonTheme.primaryColor)
                ? buttonTheme.commonTheme.onSurfaceColor
                : buttonTheme.commonTheme.surfaceColor;
        //主按钮文字颜色
        Color mainTextColor =
            HcColorUtil.needLightColor(buttonTheme.commonTheme.primaryColor)
                ? buttonTheme.commonTheme.textInverseColor
                : buttonTheme.commonTheme.textBaseColor;

        result = HcButtonTheme(
            backgroundColor:
                isMainBtn ? buttonTheme.commonTheme.primaryColor : null,
            borderRadius: finalRadius,
            fontWeight: isMainBtn ? FontWeight.w600 : FontWeight.w400,
            splashColor: isMainBtn
                ? surfaceColor
                    .withOpacity(buttonTheme.commonTheme.splashOpacity)
                : null,
            highlightColor: isMainBtn
                ? surfaceColor
                    .withOpacity(buttonTheme.commonTheme.highlightOpacity)
                : null,
            textColor: isMainBtn ? mainTextColor : null);
        break;
      case HcButtonType.outlined:
        result = HcButtonTheme(
            textColor: isMainBtn ? buttonTheme.commonTheme.primaryColor : null,
            fontWeight: isMainBtn ? FontWeight.w600 : FontWeight.w400,
            borderRadius: finalRadius,
            side: BorderSide(
                color: isMainBtn
                    ? buttonTheme.commonTheme.primaryColor
                    : borderColor ?? buttonTheme.commonTheme.borderColor),
            backgroundColor: Colors.transparent);
        break;
      case HcButtonType.text:
        result = HcButtonTheme(
            backgroundColor: Colors.transparent,
            fontWeight: isMainBtn ? FontWeight.w600 : FontWeight.w400,
            padding:
                buttonList.length == 1 || mainButtonType == secondaryButtonType
                    ? EdgeInsets.only(
                        top: padding.bottom / 2, bottom: padding.bottom / 2)
                    : null,
            //新版Flutter 好像修复了ClipRRect无法截断涟漪的问题可以直接用ClipRRect包裹
            borderRadius: mainButtonType == secondaryButtonType
                ? BorderRadius.only(
                    topLeft: Radius.zero,
                    topRight: Radius.zero,
                    bottomLeft:
                        finalDecoration == Axis.horizontal && index == 0 ||
                                index == buttonList.length - 1 &&
                                    finalDecoration == Axis.vertical
                            ? finalRadius.bottomLeft
                            : Radius.zero,
                    bottomRight:
                        finalDecoration == Axis.horizontal && index == 1 ||
                                index == buttonList.length - 1 &&
                                    finalDecoration == Axis.vertical
                            ? finalRadius.bottomRight
                            : Radius.zero,
                  )
                : BorderRadius.zero,
            textColor: isMainBtn ? buttonTheme.commonTheme.primaryColor : null);
        break;
    }
    return buttonTheme.merge(result);
  }
}
